<template>
    <div>
        <div class="bg-banner" />
        <div id="login-box">
            <!-- <div class="login-banner" /> -->
            <el-form ref="form" :model="form" :rules="rules" class="login-form" auto-complete="on" label-position="left">
                <div class="title-container">
                    <h3 class="title">{{ title }}</h3>
                </div>
                <div>
                    <el-form-item prop="userName">
                        <el-input ref="name" v-model="form.userName" placeholder="请输入用户名" type="text" tabindex="1" auto-complete="on">
                            <svg-icon slot="prefix" name="user" />
                        </el-input>
                    </el-form-item>
                    <el-form-item prop="password">
                        <el-input ref="password" v-model="form.password" :type="passwordType" placeholder="请输入密码" tabindex="2" auto-complete="on" @keyup.enter.native="handleLogin">
                            <svg-icon slot="prefix" name="password" />
                            <svg-icon slot="suffix" :name="passwordType === 'password' ? 'eye' : 'eye-open'" @click="showPassword" />
                        </el-input>
                    </el-form-item>
                    <div class="form-verification-code">
                        <el-form-item prop="verificationCode" :error="codeError">
                            <el-input ref="name" v-model="form.verificationCode" placeholder="验证码" type="text" tabindex="1" auto-complete="on" @keyup.enter.native="handleLogin">
                                <svg-icon slot="prefix" name="verification-code" />
                            </el-input>
                        </el-form-item>
                        <div class="code" @click="refreshCode">
                            <SIdentify ref="s-identify"></SIdentify>
                        </div>
                    </div>
                </div>
                <el-checkbox v-model="form.remember">记住密码</el-checkbox>
                <el-button :loading="loading" type="primary" style="width: 100%;" @click.native.prevent="handleLogin">登 录</el-button>
                <!-- <div style="margin-top: 20px; margin-bottom: -10px; color: #666; font-size: 14px; text-align: center; font-weight: bold;">
                    <span style="margin-right: 5px;">演示帐号一键登录：</span>
                    <el-button type="danger" size="mini" @click="testAccount('admin')">admin</el-button>
                    <el-button type="danger" size="mini" plain @click="testAccount('test')">test</el-button>
                </div> -->
            </el-form>
        </div>
        <Copyright v-if="$store.state.settings.showCopyright" />
    </div>
</template>

<script>
export default {
    name: 'Login',
    data() {
        return {
            title: process.env.VUE_APP_TITLE,
            form: {
                userName: localStorage.login_userName || '',
                password: '',
                verificationCode: '',
                remember: !!localStorage.login_userName
            },
            codeError: '',
            rules: {
                userName: [
                    { required: true, trigger: 'blur', message: '请输入用户名' }
                ],
                password: [
                    { required: true, trigger: 'blur', message: '请输入密码' },
                    { min: 6, max: 18, trigger: 'blur', message: '密码长度为6到18位' }
                ],
                verificationCode: [
                    { required: true, trigger: 'blur', message: '请输入验证码' },
                    { min: 4, max: 4, trigger: 'blur', message: '验证码长度为4位' }
                ]
            },
            loading: false,
            passwordType: 'password',
            redirect: undefined
        }
    },
    watch: {
        $route: {
            handler: function(route) {
                this.redirect = route.query && route.query.redirect
            },
            immediate: true
        }
    },
    methods: {
        showPassword() {
            this.passwordType = this.passwordType === 'password' ? '' : 'password'
            this.$nextTick(() => {
                this.$refs.password.focus();
            })
        },
        handleLogin() {
            this.$refs.form.validate(valid => {
                if (valid) {
                    // 获取验证码
                    let code = this.$refs['s-identify'].identifyCode
                    // 先置空
                    this.codeError = ''
                    if( this.form.verificationCode !== code ) {
                        this.$nextTick(()=>{
                            this.codeError = '验证码错误'
                        })
                        return
                    }
                    this.loading = true
                    this.$store.dispatch('user/login', this.form).then(() => {
                        this.loading = false
                        this.form.remember && localStorage.setItem('login_userName', this.form.userName)
                        this.$router.push({ path: this.redirect || '/' })
                    }).catch(() => {
                        this.loading = false
                    })
                }
            })
        },
        refreshCode() {
            this.$refs['s-identify']?.refreshCode();
            this.$nextTick(() => {
                this.$refs['s-identify']?.drawPic();
            });
        }
    }
}
</script>

<style lang="scss" scoped>
[data-mode=mobile] {
    #login-box {
        max-width: 80%;
        .login-banner {
            display: none;
        }
    }
}
::v-deep input[type=password]::-ms-reveal {
    display: none;
}
.bg-banner {
    position: absolute;
    z-index: 0;
    width: 100%;
    height: 100%;
    background-image: url(../assets/images/login-bg.png);
    background-size: 100% 100%;
    background-position: center center;
}
#login-box {
    display: flex;
    justify-content: space-between;
    position: absolute;
    top: 50%;
    left: 12.5%;
    transform: translateX(-12.5%) translateY(-50%);
    // background: rgba(255, 255, 255, 0.8);
    background-image: url(../assets/images/login-form.png);
    background-size: 100% 100%;
    background-position: center center;
    border-radius: 10px;
    overflow: hidden;
    // box-shadow: 0 0 5px #999;
    .login-banner {
        width: 300px;
        background-image: url(../assets/images/login-banner.jpg);
        background-size: cover;
        background-position: center center;
    }
    .login-form {
        width: 580px;
        padding: 50px 70px 64px;
        overflow: hidden;
        .svg-icon {
            vertical-align: -0.35em;
        }
        .title-container {
            position: relative;
            .title {
                font-size: 22px;
                color: #fff;
                margin: 0 auto 30px;
                text-align: center;
                font-weight: bold;
                @include text-overflow;
            }
        }
    }
    ::v-deep .el-input {
        display: inline-block;
        height: 50px;
        width: 100%;
        background-color: rgba(48, 122, 198, 0.8) !important;
        input {
            height: 50px;
            background-color: rgba(48, 122, 198, 0.8) !important;
            border: 1px solid rgba(48, 122, 198, 1);
            color: #fff;
        }
        .el-input__prefix {
            left: 10px;
            color: #dfe8ee;
        }
        .el-input__suffix {
            right: 10px;
            color: #dfe8ee;
        }
    }
    .el-checkbox {
        margin-bottom: 28px;
        color: #fff;
    }
    .el-form-item {
        margin-bottom: 28px;
    }
    .el-button {
        height: 50px;
    }
}
.form-verification-code {
    display: flex;
    .el-form-item {
        flex: 1;
    }
    .code {
        width: 140px;
        height: 50px;
        margin-left: 28px;
        cursor: pointer;
    }
}
.copyright {
    position: absolute;
    bottom: 30px;
    width: 100%;
    margin: 0;
    mix-blend-mode: difference;
}
</style>
